package cn.wolfcode.trip.app.web.controller;

import cn.wolfcode.trip.base.domain.Great;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.StrategyTags;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObject;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author by mycp
 * @Classname StrategyController
 * @Description TODO 大攻略资源控制器
 * @Date 2018/11/10 18:12
 */
@RestController
@RequestMapping("/strategies")
public class StrategyController {
    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @Autowired
    private IStrategyDetailService strategyDetailService;

    @Autowired
    private IGreatService greatService;

    @Autowired
    private IStrategyCommentService strategyCommentService;

    @Autowired
    private IStrategyTagsService strategyTagsService;

    @Autowired
    private ITravelService travelService;
    /**
     * 接口: 获取所有的推荐的大攻略, 不带分页
     * /strategies
     * 动作: GET
     * 参数: state = 2&pageSize = 0;
     */
    @GetMapping
    public Object list(StrategyQueryObject qo){
        return strategyService.query(qo);
    }

    /**
     * 接口: 获取指定大攻略id 的大攻略
     * /strategies/{id}
     * 动作: GET
     * 参数: id = xx
     */
    @GetMapping("/{id}")
    public Object get(@PathVariable Long id){
        Strategy strategy = strategyService.get(id);
        return strategy;
    }

    /**
     * 接口: 获取指定大攻略下的所有不被禁用的攻略分类(不分页)
     * /strategies/{strategyId}/catalogs
     * 动作: GET
     * 参数: strategyId = xx
     */
    @GetMapping("/{strategyId}/catalogs")
    public Object getCatalogsByStrategyId(@PathVariable Long strategyId){
        return strategyCatalogService.selectEnable(strategyId);
    }

    /**
     * 接口: 获取指定 id 的攻略文章
     * /strategies/details/{id}
     * 动作: GET
     * 参数: id = xx
     */
    @GetMapping("/details/{detailsId}")
    public Object getDetail(@PathVariable Long detailsId){
        return strategyDetailService.get(detailsId);
    }


    /**
     * 接口: /strategies/{id}/greats
     * 动作: GET
     * 参数: greatType="travel_id"
     * 获取指定攻略的点赞数
     */
    @GetMapping("/{greatTypeId}/greats")
    public Object getTravelGreats(String greatType,@PathVariable Long greatTypeId){
        Map<String, Object> map = new HashMap<>();
        Integer greats = greatService.queryForTotalCount(greatType,greatTypeId);
        map.put("greats", greats);
        return map;
    }

    /**获取某篇攻略的点赞数
     * 接口:/strategies/{id}/greats
     * 动作:post
     * 参数:greatType="travel_id"&userId=xxx
     */
    @PostMapping("/{greatTypeId}/greats")
    public Object great(String greatType,@PathVariable Long greatTypeId, Great entity){
        // 定义返回的数据
        Map<String, Object> map = new HashMap<>();
        // 查询是否有该用户的点赞记录
        Great great = greatService.selectGreatByGreatTypeIdAndUserId(greatType,greatTypeId, entity.getUserId());
        if (great != null) {
            // 如果找到了这条记录, 则删除掉这条点赞
            greatService.removeGreatByGreatTypeIdAndUserId(greatType,greatTypeId, entity.getUserId());
        } else {
            // 如果找不到这条记录, 则添加一条点赞数
            greatService.saveGreatByGreatTypeIdAndUserId(greatType,greatTypeId, entity);
        }
        // 重新查询游记点赞数
        Integer greats = greatService.queryForTotalCount(greatType,greatTypeId);
        // 设置响应数据
        map.put("greats", greats);
        return map;
    }


    /**
     * 根据攻略id获取某篇攻略的点评
     * 接口:/strategies/{id}/strategyComments
     * 动作:GET
     * 参数:state=2&pageSize=2&currentPage=x
     */
    @GetMapping("/{strategyId}/strategyComments")
    public Object getStrategyCommentsByStrategyId(StrategyCommentQueryObject qo){
        qo.setOrderBy("scm.great_count desc,scm.releaseTime desc");
        return strategyCommentService.query(qo);
    }

    /**
     * 获取所有的头条攻略点评
     * 接口:/strategies/strategyComments/commends
     * 动作:GET
     * 参数:state=2&isCommend=1
     */
    @GetMapping("/strategyComments/commends")
    public Object getStrategyCommentCommends(StrategyCommentQueryObject qo){
        qo.setOrderBy("scm.great_count desc,scm.releaseTime desc");
        return strategyCommentService.query(qo);
    }

    /**
     * 根据作者id查询攻略点评的数量
     * 接口:/strategies/{id}/strategyComments
     * 动作:post
     * 参数:state=2&pageSize=2&currentPage=x
     */
    @GetMapping("/{authorId}/commentCounts")
    public Object getStrategyCommentCountsByStrategyId(StrategyCommentQueryObject qo){
        Integer count = strategyCommentService.getCountByAuthorId(qo);
        return count;
    }

    /**
     * 根据攻略id获取某篇攻略的标签
     * 接口:/strategies/{id}/tags
     * 动作:GET
     * 参数:
     */
    @GetMapping("/{strategyId}/tags")
    public Object getTagsByStrategyId(@PathVariable Long strategyId){
        List<StrategyTags> strategyTags = strategyTagsService.listNeedByStrategyId(strategyId);
        return strategyTags;
    }

    /**
     * 接口:获取大攻略下所有的游记
     * 资源:/strategies/{strategyId}/travels
     * 动作:GET
     * 参数:strategyId=xx
     */
    @GetMapping("/{strategyId}/travels")
    public Object getTravel(@PathVariable Long strategyId){
        return travelService.getTravel(strategyId);
    }

}
